<style lang="scss" scoped>

.utitle {
  padding-top: 10px;
  width: 100%;
  height: 60px;
  text-align: center;
  background: #fff;
}

.mtitle {
  font-size: 18px;
  font-weight: bold;
  color: #000;
}

.ztitle {
  font-size: 8px;
  color: #999;
}

.nav_section {
  width: 100%;
}

.nav_section_items {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 15px;
  border-bottom: 1px solid #ddd;
  position: relative;
}

.nav_section_items:active {
  background: #ddd;
}

.nav_section_items .section_image {
  width: 85px;
  height: 80px;
}

.nav_section_items .section_image img {
  width: 100%;
  height: 100%;
}

.nav_section_items .section_cont {
  width: 200px;
  font-size: 13px;
  color: #a9a9a9;

}

.nav_section_items .section_cont div {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.nav_section_items .section_cont .section_cont_intro {
  white-space: normal;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.nav_section_items .section_cont .section_cont_sub {
  font-size: 15px;
  line-height: 30px;
  color: #000;
  margin-bottom: 5px;
}

.nav_section_items .section_cont .section_cont_price {
  font-size: 16px;
  color: #d24a58;
}

.nav_section_items .section_book {
  width: 40px;
  height: 40px;
  font-size: 13px;
  border-radius: 100%;
  background: #d24a58;
  line-height: 40px;
  color: #fff;
  text-align: center;
  margin-top: 18px;
}
</style>

<template>
  <div ref="container" class="container hasNav hasTabbar">

    <van-nav-bar title="美容服务" fixed />

    <van-swipe :height="210" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="(item, index) in banners" :key="index">
        <van-image fit="cover" :src="fileApi + item.picUrl"></van-image>
      </van-swipe-item>
    </van-swipe>

    <div class="utitle">
      <div class="mtitle">全部服务</div>
      <div class="ztitle">service</div>
    </div>

    <div class="nav_section">
      <template v-for="(item, index) in serviceList">
        <div class="nav_section_items">
          <div class="section_image" @click="linkServiceDetailTo(item)">
            <img :src="fileApi + item.picUrl" />
          </div>
          <div class="section_cont" @click="linkServiceDetailTo(item)">
            <div class="section_cont_sub">
              <span>{{ item.serviceName }}</span>
            </div>
            <div class="section_cont_price">价格：￥{{ item.linePrice }}</div>
            <div>{{ item.brief }}</div>
          </div>
          <div class="section_book" @click="linkReservationTo(item)">
            <span>预约</span>
          </div>
        </div>
      </template>
    </div>

    <Loading :isShow="loading"></Loading>
    <!-- 返回顶部 -->
    <back-top selector=".container"></back-top>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex';
import mixin from '@/mixin/index';
import Loading from '@/components/Loading';
import Tabbar from '@/components/Tabbar';
import BackTop from '@/components/BackTop';
import { fileUrl, getBannerList, getServiceData } from '../../api/service';

export default {
  name: 'service',
  components: {
    BackTop,
    Loading,
    Tabbar,
  },
  mixins: [mixin],
  data() {
    return {
      fileApi: fileUrl,
      loading: true,
      banners: [],
      pageIndex: 1,
      pageSize: 10,
      isCompleted: false,
      serviceList: [],
    };
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  beforeMount() {
    this.getServiceBanner();
    this.getServiceData();
  },
  mounted() {
    this.bindScroll('container', () => {
      if (!this.isCompleted) {
        this.pageIndex += 1;
        this.getServiceData();
      }
    });
  },
  methods: {
    ...mapMutations(['setBanners']),

    async getServiceBanner() {
      try {
        this.loading = true;
        const { code, message, data } = await getBannerList({ position: 2 });
        if (code == 200) {
          if (data.length !== 0) {
            this.banners = data;
            this.setBanners(data);
          }
        } else {
          this.$toast(message);
        }
        this.$nextTick(() => {
          this.loading = false;
        });
      } catch (error) {
        console.log(error);
        this.loading = false;
      }
    },

    async getServiceData() {
      try {
        this.loading = true;
        const { code, message, data } = await getServiceData({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        });
        if (code == 200) {
          if (data.records.length === 0) {
            this.isCompleted = true;
          } else {
            this.serviceList.push(...data.records);
          }
        } else {
          this.$toast(message);
        }
        this.$nextTick(() => {
          this.loading = false;
        });
      } catch (error) {
        console.log(error);
        this.loading = false;
      }
    },

    linkServiceDetailTo({ id }) {
      this.$router.push({
        path: '/service/detail',
        query: { id }
      });
    },

    linkReservationTo({ id }) {
      this.$router.push({
        path: '/service/reservation',
        query: { id }
      }, onComplete => {});
    },
  }
};
</script>